
//换行
fn next_line(s: &mut String, deep: u8){
    s.push('\n');
    for _ in 0..deep {
        s.push_str("    ");
    }
}
//json美化
pub fn pretty_json(json: &String) -> String {
    let mut result = String::new();
    let mut deep = 0_u8;
    for c in json.chars() {
        if c == ' ' || c == '\n' {
            continue;
        }
        if c == '}' || c == ']' {
            deep -= 1;
            next_line(&mut result, deep);
            result.push(c);
            continue;
        }
        result.push(c);
        if c == '{' || c == '['{
            deep += 1;
            next_line(&mut result, deep);
            continue;
        }
        if c == ',' {
            next_line(&mut result, deep);
            continue;
        }
        if c == ':' {
            result.push(' ');
        }

    }
    return result;
}

#[test]
pub fn pretty_json_demo(){
    let json: String = String::from(r#"{"name"："李四",
     "age": 23,
     "parent": {"name": "王五", "age":45},
     "sisters":[{"name":"张三",  "age":22}, {"name":"陈留", "age":12}],
     "likes":["football","basketball"]}"#);
    let s = pretty_json(&json);
    println!("{}", s);
}